import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function (location) {
  return originalPush.call(this, location).catch(err => { })
};
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: "/home"
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    meta: {
      isShowTabbar: true,
    },
    children: [
      {
        path: '/home/SearchPopup',
        name: 'SearchPopup',
        component: () => import('../views/SearchPopup.vue')
      }
    ]
  },
  {
    path: '/topic',
    name: 'Topic',
    meta: {
      activeNum: 1,
      isShowTabbar: true,
    },
    component: () => import('../views/Topic.vue'),
  },
  {
    path: '/category',
    name: 'Category',
    meta: {
      activeNum: 2,
      isShowTabbar: true,
    },
    component: () => import('../views/Category.vue'),
  },
  {
    path: '/cart',
    name: 'Cart',
    meta: {
      activeNum: 3,
      isShowTabbar: true,
    },
    component: () => import('../views/Cart.vue'),
  },
  {
    path: '/user',
    name: 'User',
    meta: {
      activeNum: 4,
      isShowTabbar: true,
    },
    component: () => import('../views/User.vue'),
  },
  {
    path: '/productDetail',
    name: 'ProductDetail',
    meta: {
      activeNum: 5
    },
    component: () => import('../views/ProductDetail.vue'),
  },



  // {
  //   path: '/about',
  //   name: 'About',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  // }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 路由守卫 
router.beforeEach((to, from, next) => {
  // 要去哪
  console.log("to", to);
  // 从哪来
  console.log("from", from);
  let token = localStorage.getItem("token");
  if (to.path == '/cart') {
    if (token) {
      next();
    } else {
      Vue.prototype.$toast("请先登录");
      setTimeout(() => {
        next('/user');
      }, 1000);
    }
    return
  }
  // 放行
  next();
})

export default router
